package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.commom.enums.Sex;
import com.shop.commom.utils.MD5Utils;
import com.shop.common.exception.MyException;
import com.shop.mapper.UsersMapper;
import com.shop.pojo.Users;
import com.shop.service.UsersService;
import com.shop.vo.UsersQueryRequest;
import com.shop.vo.UsersSaveRequest;
import com.shop.vo.UsersUpdateRequest;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;


@Service("usersService")
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private Sid sid;

    @Override
    public IPage<Users> page(Page page, UsersQueryRequest request){
        QueryWrapper<Users> wrapper = Users.queryWrapper(request);
        return this.page(page,wrapper);
    }

    @Transactional
    @Override
    public void save(UsersSaveRequest request){
        Users users = new Users();
        BeanUtils.copyProperties(request,users);
        this.save(users);
    }

    @Transactional
    @Override
    public void update(UsersUpdateRequest request){
        Users users = new Users();
        BeanUtils.copyProperties(request,users);
        this.updateById(users);
    }

    @Transactional
    @Override
    public void delete(String id){
        UpdateWrapper<Users> updateWrapper = new UpdateWrapper();
        this.update(updateWrapper);
    }

    @Override
    public boolean queryUserNameIsExist(String username) {
        QueryWrapper<Users> query = new QueryWrapper<>();
        query.eq(Users.USERNAME,username);
        Users one = this.getOne(query);
        return one!=null;
    }

    @Transactional
    @Override
    public Users createUser(UsersSaveRequest request) throws NoSuchAlgorithmException {
        if(this.queryUserNameIsExist(request.getUsername())){
            throw new MyException("用户名已经存在!");
        }
        Users user = new Users();
        BeanUtils.copyProperties(request,user);
        user.setFace(request.getUsername());
        user.setPassword(MD5Utils.getMD5Str(user.getPassword()));
        // user.setId(sid.nextShort());
        user.setSex(Sex.SECRET.type);
        this.save(user);
        return user;
    }

    @Override
    public Users queryUserForLogin(String username, String password) throws NoSuchAlgorithmException {
        QueryWrapper<Users> query = new QueryWrapper<>();
        query.eq(Users.USERNAME,username);
        query.eq(Users.PASSWORD,MD5Utils.getMD5Str(password));
        Users one = this.getOne(query);
        if (one==null){
            throw new MyException("用户名或密码不正确");
        }
        return one;
    }
}